import React from 'react'
import PropTypes from 'prop-types'
import {routerRedux} from 'dva/router'
import {connect} from 'dva'
import {Card, Modal, Row} from 'antd'
import {Page} from 'components'
import queryString from 'query-string'
import List from './components/List'
import Filter from './components/Filter'

const ChatroomMember = ({
                          location, dispatch, chatroomMember, loading,
                        }) => {
  const {query, pathname} = location
  const {
    list, pagination, statList, expandForm, currentItem, modalVisible, modalType, selectedRowKeys,
  } = chatroomMember

  const handleRefresh = (newQuery) => {
    dispatch(routerRedux.push({
      pathname,
      search: queryString.stringify({
        ...query,
        ...newQuery,
      }),
    }))
  }

  const modalProps = {
    item: modalType === 'create' ? {} : currentItem,
    visible: modalVisible,
    width: 1600,
    maskClosable: false,
    confirmLoading: loading.effects[`chatroomMember/${modalType}`],
    title: '新增微信运营号',
    wrapClassName: 'vertical-center-modal',
    onOk (data) {
      dispatch({
        type: `chatroomMember/${modalType}`,
        payload: data,
      })
        .then(() => {
          handleRefresh()
        })
    },
    onCancel () {
      dispatch({
        type: 'chatroomMember/hideModal',
      })
    },
  }

  const listProps = {
    rowKey: 'id',
    dataSource: list,
    loading: loading.effects['chatroomMember/query'],
    pagination,
    dispatch,
    location,
    onChange (page) {
      handleRefresh({
        current: page.current,
        size: page.pageSize,
      })
    },
    openTab (record) {
      const tab = {
        name: '群成员-'.concat(record.inChatroomNickname || record.memberNickname),
        route: '/customer/'.concat(record.id),
      }
      dispatch({
        type: 'app/addTab',
        payload: {
          menu: tab,
        },
      })
    },
    rowSelection: {
      selectedRowKeys,
      onChange: (keys) => {
        dispatch({
          type: 'chatroomMember/updateState',
          payload: {
            selectedRowKeys: keys,
          },
        })
      },
    },
  }

  const filterProps = {
    statList,
    filter: {
      ...query,
    },
    expandForm,
    toggleForm () {
      dispatch({type: 'chatroomMember/toggleForm'})
    },
    onFilterChange (value) {
      handleRefresh({
        ...value,
        current: 1,
      })
    },
  }

  return (
    <div>
      <Row gutter={24}>
        <Card style={{marginBottom: 12}}>
          <Filter {...filterProps}/>
        </Card>
      </Row>
      <Row gutter={24}>
        <Page inner>
          <List {...listProps} />
          {modalVisible && <Modal {...modalProps} />}
        </Page>
      </Row>
    </div>
  )
}

ChatroomMember.propTypes = {
  chatroomMember: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func,
  loading: PropTypes.object,
}

export default connect(({chatroomMember, loading}) => ({chatroomMember, loading}))(ChatroomMember)
